package com.yanq.liancar.adapter;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/10/10  14:33
 *  @描述       ${TODO}
 */


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.ReplyAllActivity;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.CommentsLikesBean;
import com.yanq.liancar.bean.FindBean;
import com.yanq.liancar.bean.FindCommentTwoBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class FindCommentAdapter extends RecyclerView.Adapter<FindCommentAdapter.MyViewHolder> {
    private Context mContext;
    private List<FindCommentTwoBean.DataBean> mList;
    private LayoutInflater mInflater;
    private List<FindCommentTwoBean.DataBean.CommentsRepliesBean> mRepliesBeanList;
    private int likeNum;
    private OnItemClickListener onClickListener;
    public FindCommentAdapter(Context context, List<FindCommentTwoBean.DataBean> dataBeanList) {
        this.mContext = context;
        this.mList = dataBeanList;
        mInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(mContext).inflate(R.layout.big_findcomment, null, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        //holder.mImageView.setImageResource();
        holder.tv_name.setText(mList.get(position).getCommentatorname());
        holder.tv_commentcontent.setText(mList.get(position).getCommentscontent());
        holder.tv_time.setText(mList.get(position).getCommentscreatetime());
        holder.tv_like.setText(mList.get(position).getCommentslikesnum() + "");
        likeNum = mList.get(position).getCommentslikesnum();
        holder.tv_comment.setText(mList.get(position).getCommentsnum() + "");
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.vip_touxiang);
        Glide.with(mContext).load(mList.get(position).getHeadUrl()).apply(options).into(holder.headimg);
        if (holder.mRecyclerView.getAdapter() == null) {
            if (mList.get(position).getComments_Replies().size() > 2) {
                mRepliesBeanList = new ArrayList<>();
                for (int i = 0; i < 2; i++) {
                    mRepliesBeanList.add(mList.get(position).getComments_Replies().get(i));
                }
            }else {
                mRepliesBeanList = new ArrayList<>();
                for (int i = 0; i < mList.get(position).getComments_Replies().size(); i++) {
                    mRepliesBeanList.add(mList.get(position).getComments_Replies().get(i));
                }
            }
            holder.mRecyclerView.setAdapter(new SmallFindCommentAdapter(mContext, mRepliesBeanList, mList.get(position).getCommentatorid()));
        }/*else { holder.mRecyclerView.getAdapter().notifyDataSetChanged(); }*/

        LogUtils.e("Comments_Replies().size()", mList.get(position).getComments_Replies().size() + "");
        if (mList.get(position).getComments_Replies().size() > 2) {
            holder.tv_more.setVisibility(View.VISIBLE);
        } else {
            holder.tv_more.setVisibility(View.GONE);
        }

        //查看更多评论
        holder.tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("commentsid", mList.get(position).getCommentsid());
                RxActivityTool.skipActivity(mContext, ReplyAllActivity.class,bundle);
            }
        });

        //评论点赞
        holder.tv_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //initDianzan(mList.get(position).getNewsid(),mList.get(position).getCommentsid());
                //点赞，取消点赞
                OkGo.<String>post(Config.s + "llc/api/insert_commentsLikes")
                        .tag(this)
                        .params("newsid", mList.get(position).getNewsid() + "")
                        .params("commentsid", mList.get(position).getCommentsid() + "")
                        .params("token", UserBean.getcToken(mContext))
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                Gson gson = new Gson();
                                CommentsLikesBean bean = gson.fromJson(response.body(), CommentsLikesBean.class);
                                if (bean.isIsSuccess()) {
                                    if (bean.getCode()==200){
                                        holder.tv_like.setText(bean.getData()+"");
                                    }else {
                                        RxToast.error("操作失败");
                                    }
                                } else {
                                    RxToast.info(bean.getOutputMessage());
                                }
                            }
                        });
            }
        });

        holder.headimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.OnItemClick(view,position);
            }
        });

        holder.tv_commentcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.OnItemClick(view,position);
            }
        });

        holder.tv_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.OnItemClick(view,position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return (mList == null ? 0 : mList.size());

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView mRecyclerView;
        private TextView tv_name;
        private TextView tv_commentcontent;
        private TextView tv_time;
        private TextView tv_like;
        private TextView tv_comment;
        private TextView tv_more;
        private ImageView headimg;

        public MyViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = itemView.findViewById(R.id.recyclerview);
            RecyclerView.LayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
            manager.setAutoMeasureEnabled(true);
            mRecyclerView.setNestedScrollingEnabled(false);
            mRecyclerView.setLayoutManager(manager);

            tv_name = itemView.findViewById(R.id.tv_name);
            tv_commentcontent = itemView.findViewById(R.id.tv_commentcontent);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_like = itemView.findViewById(R.id.tv_like);
            tv_comment = itemView.findViewById(R.id.tv_comment);
            headimg = itemView.findViewById(R.id.headimg);
            tv_more = itemView.findViewById(R.id.tv_more);
        }
    }

    public void setData(List<FindCommentTwoBean.DataBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void OnItemClick(View view ,int postion);
    }

    public void setOnClickListener(OnItemClickListener listener){
        this.onClickListener = listener;
    }
}
